﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using ERPeasy.Common;

namespace ERPeasy.Config
{
    public enum InstallationTypeEnum
    {
        Portable,
        JustMe,
        Everyone
    }

    public static class GlobalConfig
    {
        private const string IniFileName = "config.ini";

        public static string ConfigFileName
        {
            get
            {
                // Zuerst im Anwendungspfad suchen (für Portable-Versionen)
                if (InstallationType == InstallationTypeEnum.Portable)
                    return ConfigFileNamePortable;
                // Dann im AppData-Pfad suchen (Standardinstallation für "Just me")
                else if (InstallationType == InstallationTypeEnum.JustMe)
                    return ConfigFileNameJustMe;
                // Dann im ProgramData-Pfad suchen. (Standardinstallation für "Everyone")
                else if (InstallationType == InstallationTypeEnum.Everyone)
                    return ConfigFileNameEveryone;
                else
                    return ConfigFileNameJustMe;
            }
        }

        public static InstallationTypeEnum InstallationType
        {
            get
            {
                if (File.Exists(ConfigFileNamePortable))
                    return InstallationTypeEnum.Portable;
                else if (File.Exists(ConfigFileNameJustMe))
                    return InstallationTypeEnum.JustMe;
                else if (File.Exists(ConfigFileNameEveryone))
                    return InstallationTypeEnum.Everyone;
                else
                    return InstallationTypeEnum.JustMe;
            }
        }

        private static string ConfigFileNamePortable
        {
            get
            {  
                return Path.GetFullPath(ERPeasy.Common.SpecialFolders.ApplicationPath + "\\..\\config\\" + IniFileName);
            }
        }

        private static string ConfigFileNameJustMe
        {
            get
            {
                return Path.GetFullPath(ERPeasy.Common.SpecialFolders.AppDataPath + "\\config\\" + IniFileName);
            }
        }

        private static string ConfigFileNameEveryone
        {
            get
            {
                return Path.GetFullPath(ERPeasy.Common.SpecialFolders.ProgramDataPath + "\\config\\" + IniFileName);
            }
        }

        public static ConnectionConfig Connection
        {
            get
            {
                return new ConnectionConfig(ConfigFileName);
            }
        }
    }

    public class ConnectionConfig
    {
        private string ConfigFileName;

        public ConnectionConfig(string configFileName)
        {
            this.ConfigFileName = configFileName;
        }

        public string Hostname
        {
            get
            {
                return new IniFile(ConfigFileName).Read("connection", "hostname", "localhost");
            }
            set
            {
                new IniFile(ConfigFileName).Write("connection", "hostname", value);
            }
        }

        public short Port
        {
            get
            {
                return short.Parse(new IniFile(ConfigFileName).Read("connection", "port", "3050"));
            }
            set
            {
                new IniFile(ConfigFileName).Write("connection", "port", value.ToString());
            }
        }

        public string Database
        {
            get
            {
                return new IniFile(ConfigFileName).Read("connection", "database", "");
            }
            set
            {
                new IniFile(ConfigFileName).Write("connection", "database", value);
            }
        }

        public string Username
        {
            get
            {
                return new IniFile(ConfigFileName).Read("connection", "username", "SYSDBA");
            }
            set
            {
                new IniFile(ConfigFileName).Write("connection", "username", value);
            }
        }

        public string Password
        {
            get
            {
                return new IniFile(ConfigFileName).Read("connection", "password", "masterkey");
            }
            set
            {
                new IniFile(ConfigFileName).Write("connection", "password", value);
            }
        }
    }
}
